<template>
  <div class="app-container">
    <el-card class="box-card">
    <h3 class="title-left">花名册</h3>
    <el-divider style="border-width: thin"></el-divider>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同编号" prop="cno">
        <el-input
          v-model="queryParams.cno"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择员工类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_emplyee_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择学历" clearable>
          <el-option
            v-for="dict in dict.type.sys_education"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:emplyee:add']">新增</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:emplyee:remove']">删除</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="12">
      <el-col :span="6">
        <el-card shadow="always">
          在职
          <span class="bt1" style="margin-left: 30px">2</span>
          <span class="bt2 txt1" style="margin-left: 10px">（个）</span>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="always">
          使用
          <span class="bt1" style="margin-left: 30px">1</span>
          <span class="bt2 txt1" style="margin-left: 10px">（个）</span>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="always">
          待离职
          <span class="bt1" style="margin-left: 30px">2</span>
          <span class="bt2 txt1" style="margin-left: 10px">（个）</span>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="always">
          已离职
          <span class="bt1" style="margin-left: 30px">4</span>
          <span class="bt2 txt1" style="margin-left: 10px">（个）</span>
        </el-card>
      </el-col>
    </el-row>

    <el-table stripe v-loading="loading" :data="emplyeeList" @selection-change="handleSelectionChange" class="eltable">
      <el-table-column type="selection" width="25" align="center" />
      <el-table-column label="头像/姓名" align="center" prop="name" width="150px">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avator" :width="30" :height="30" style="border-radius: 50px"/>
          <span style="position: relative;bottom: 8px;left: 6px">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工编号" align="center" prop="eno" width="140px"/>
      <el-table-column label="年龄" align="center" prop="age" width="50px"/>
      <el-table-column label="性别" align="center" prop="gender" width="50px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" width="120px"/>

      <el-table-column label="证件号码" align="center" prop="idno" width="164px"/>
      <el-table-column label="员工状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_emplyee_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="员工类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_emplyee_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_education" :value="scope.row.education"/>
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="political">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_political" :value="scope.row.political"/>
        </template>
      </el-table-column>
      <el-table-column label="微信" align="center" prop="wechat" width="90px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleLook(scope.row)">查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:emplyee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同工对话框  https://jsonplaceholder.typicode.com/posts/-->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="4" :offset="8">
            <el-form-item prop="avator" label="头像上传" required>
              <image-upload v-model="form.avator" style="width: 70px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="合同编号" prop="cno">
              <el-input v-model="form.cno" placeholder="请输入合同编号" />
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="员工姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入员工姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄" />
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="民族" prop="nation">
              <el-input v-model="form.nation" placeholder="请输入民族" />
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="证件号码" prop="idno">
              <el-input v-model="form.idno" placeholder="请输入证件号码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="员工状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择员工状态">
                <el-option
                  v-for="dict in dict.type.sys_emplyee_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="员工类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择员工类型">
                <el-option
                  v-for="dict in dict.type.sys_emplyee_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="学历" prop="education">
              <el-select v-model="form.education" placeholder="请选择学历">
                <el-option
                  v-for="dict in dict.type.sys_education"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="政治面貌" prop="political">
              <el-select v-model="form.political" placeholder="请选择政治面貌">
                <el-option
                  v-for="dict in dict.type.sys_political"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="微信" prop="wechat">
              <el-input v-model="form.wechat" placeholder="请输入微信" />
            </el-form-item>
          </el-col>
          <el-col :span="10" offset="2">
            <el-form-item label="QQ" prop="qq">
              <el-input v-model="form.qq" placeholder="请输入QQ" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="籍贯" prop="nativePlace">
              <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>
<style>
.title-left{
  position: relative;
  padding-left: 13px;
  margin: 2px 0
}
.title-left:before{
  content: "";
  background-color: #3796EC;
  width: 5px;
  height: 17px;
  position: absolute;
  left: 5px;
  top: 45%;
  margin: -8px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
}
.eltable{
  margin-top: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 70px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;

}
.avatar-uploader-icon {
  border-radius: 70px;
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 70px;
}
.bt1 {
  font-weight: 700;
  font-size: 32px;
  line-height: 37px;
}
.bt_top {
  margin-top: 16px;
}
.bt_top1 {
  margin-top: 8px;
}
.bt2 {
  font-size: 12px;
  line-height: 14px;
}
.txt1 {
  font-size: 14px;
  line-height: 16px;
  color: #999999;
}
</style>
<script>
import { listEmplyee, getEmplyee, delEmplyee, addEmplyee, updateEmplyee } from "@/api/system/emplyee";
import EmpDetail from '@/views/person/sgang/emplyee/empDetail'
import ImageUpload from '@/components/ImageUpload'
import { getToken } from "@/utils/auth";
export default {
  name: "Emplyee",
  dicts: ['sys_education', 'sys_emplyee_status', 'sys_political', 'sys_emplyee_type', 'sys_user_sex'],
  components:{
    EmpDetail:EmpDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //图片地址
      avator: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同工表格数据
      emplyeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层  process.env.VUE_APP_BASE_API +
      open: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cno: null,
        name: null,
        type: null,
        education: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gender: [
          { required: true, message: "性别：1男，2女，3其他不能为空", trigger: "change" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同工列表 */
    getList() {
      this.loading = true;
      listEmplyee(this.queryParams).then(response => {
        this.emplyeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eno: null,
        cno: null,
        name: null,
        age: null,
        gender: null,
        phone: null,
        email: null,
        nation: null,
        idno: null,
        status: null,
        type: null,
        education: null,
        political: null,
        nativePlace: null,
        wechat: null,
        qq: null,
        avator:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //上传头像成功回调
    handleAvatarSuccess(res, file) {
      this.form.avator = URL.createObjectURL(file.raw);
      console.log(this.form.avator);
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eno)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同工";
    },
    /* 查看员工详情 */
    handleLook(row){
      console.log(row);
      this.$router.push("/person/empDetail/index/" + row.eno);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eno = row.eno || this.ids
      getEmplyee(eno).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eno != null) {
            updateEmplyee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);
            addEmplyee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const enos = row.eno || this.ids;
      this.$modal.confirm('是否确认删除合同工编号为"' + enos + '"的数据项？').then(function() {
        return delEmplyee(enos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/emplyee/export', {
        ...this.queryParams
      }, `emplyee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
